//
//  CSInputView.m
//  市场监管通
//
//  Created by 网新中研 on 16/4/22.
//  Copyright © 2016年 centralsoft. All rights reserved.
//

#import "CSInputView.h"
#import "Common.h"
#import "CS_Page_TextFieldView.h"
#import "CSPageElementModel.h"

@interface CSInputView()

@property (nonatomic, strong) UITextField *inputTextField;

@property (nonatomic, strong) UIView *seperatorLine;

@property (nonatomic, strong) NSString *titleString;

@property (nonatomic, strong) NSString *placeHolder;

@property (nonatomic, assign) UIKeyboardType keyboardType;

@property (nonatomic, strong) NSString *data;

@property (nonatomic, strong) CS_Page_TextFieldView *page_textFieldView;

@end

@implementation CSInputView

-(NSInteger)viewType{
    return CSViewType_PageTextFieldView;
}

+(instancetype)inputViewWith:(NSString *)title keyboardType:(UIKeyboardType)keyboardType{
    CSInputView *inputView = [CSInputView new];
    title = [title replaceCharcter:@"：" withCharcter:@""];
    inputView.page_textFieldView = [CS_Page_TextFieldView textFiledWithPlaceHolder:title keyboardType:keyboardType];
    return inputView;
    
    
    inputView.titleString = title;
    inputView.keyboardType = keyboardType;
    [inputView reset];
    return inputView;
}

+(instancetype)inputViewWith:(NSString *)title keyboardType:(UIKeyboardType)keyboardType data:(NSString *)data{
    CSInputView *inputView = [CSInputView new];
    title = [title replaceCharcter:@"：" withCharcter:@""];
    inputView.page_textFieldView = [CS_Page_TextFieldView textFiledWithPlaceHolder:title keyboardType:keyboardType data:data];
    return inputView;
    
    inputView.titleString = title;
    inputView.keyboardType = keyboardType;
    inputView.data = data;
    [inputView reset];
    return inputView;
}

+(instancetype)inputViewWith:(NSString *)title placeHolder:(NSString *)placeHolder keyboardType:(UIKeyboardType)keyboardType data:(NSString *)data{
    CSInputView *inputView = [CSInputView new];
    title = [title replaceCharcter:@"：" withCharcter:@""];
    inputView.page_textFieldView = [CS_Page_TextFieldView textFiledWithPlaceHolder:title keyboardType:keyboardType data:data];
    return inputView;
    
    
    inputView.titleString = title;
    inputView.keyboardType = keyboardType;
    inputView.data = data;
    inputView.placeHolder = placeHolder;
    [inputView reset];
    return inputView;
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    return [_page_textFieldView layoutView:superView primaryY:primaryY];
    
    
    CGFloat localY = primaryY;
    
    //标题
    CGFloat estimateDescriptionHeight = [self estimateHeight:self.titleString font:self.descriptionLabel.font width:CELL_TITLE_WIDTH];
    CGFloat finalDescriptionHeight = estimateDescriptionHeight > CELL_LABLE_HEIGHT? estimateDescriptionHeight : CELL_LABLE_HEIGHT;
    self.descriptionLabel.frame = CGRectMake(0, primaryY + CELL_TOP_MARGIN, CELL_TITLE_WIDTH, finalDescriptionHeight);
    self.descriptionLabel.text = [self isNullOrEmpty:self.titleString]?@"":[NSString stringWithFormat:@"%@", self.titleString];
    
    //输入值
    self.inputTextField.frame = CGRectMake(CELL_TITLE_WIDTH, primaryY + CELL_TOP_MARGIN, kWindowW - CELL_TITLE_WIDTH, CELL_LABLE_HEIGHT);
    self.inputTextField.text = self.data;
    self.inputTextField.placeholder = self.placeHolder;
    self.inputTextField.keyboardType = self.keyboardType;
    
    [superView addSubview:self.descriptionLabel];
    [superView addSubview:self.inputTextField];
    
    localY = primaryY + CELL_TOP_MARGIN + finalDescriptionHeight + CELL_BOTTOM_MARGIN;
    
    self.seperatorLine.frame = CGRectMake(CELL_SEPERATOR_MARGIN_LEFT, localY, kWindowW-CELL_SEPERATOR_MARGIN_LEFT, CELL_SEPERATOR_LINE_HEIGHT);
    
    [superView addSubview:self.seperatorLine];
    
    return [super layoutView:superView primaryY:localY + CELL_SEPERATOR_LINE_HEIGHT];
}

- (UIView *)seperatorLine {
	if(_seperatorLine == nil) {
		_seperatorLine = [[UIView alloc] init];
        STYLE_SEPERATOR_LINE(_seperatorLine);
    }
	return _seperatorLine;
}

- (UITextField *)inputTextField {
	if(_inputTextField == nil) {
		_inputTextField = [[UITextField alloc] init];
        _inputTextField.font = [UIFont boldSystemFontOfSize:15.0];
        _inputTextField.textColor = [UIColor colorFromHexCode:@"000"];
	}
	return _inputTextField;
}

-(void)reset{
    self.inputTextField.text = @"";
}

-(NSString *)value{
    
    return _page_textFieldView.value;
    
    return self.inputTextField.text;
}

@end
